package com.atguigu.gmall.search.listener;

import com.atguigu.gmall.search.service.SearchService;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class SpuInfoListener {
    @Autowired
    SearchService searchService;
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "item_spu_queue",durable = "true")
            ,exchange =@Exchange(
                    value = "PMS_SPU_EXCHANGE",
                    ignoreDeclarationExceptions = "true",
                    type = ExchangeTypes.TOPIC),
        key = "item.save"))
    private void listener(Long id, Channel channel, Message message) throws IOException {
        if (id==null){
            return;
        }
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        //创建索引
        searchService.createIndex(id);


    }
}
